package com.tueye.utils;

import java.io.File;
import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 *  the util of handle the file
 * @author yuyue
 *
 */
		
public class FileHandle implements  Serializable
{

	private static final long serialVersionUID = -6566961157461814704L;

	public String commonPath(String path) {
		char[] ch = path.toCharArray();

		for (int i = 0; i < ch.length; i++) {
			if (ch[i] == '/' || ch[i] == '\\') {
				ch[i] = File.separatorChar;
			}
		}
		return String.valueOf(ch);
	}
	
	public File checkIsAbsolute(String AbsolutePath) {
		File file = new File(commonPath(AbsolutePath));
		if (file.isAbsolute()) {
			return file;
		} else {
			
			try {
				throw new FileException(" ");
			} catch (FileException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	public   File checkisDirectory(String AbsolutePath) {
		File file = new File(commonPath(AbsolutePath));
		if (file.isAbsolute()) {
			if(file.isDirectory())
			{
				return file;
			}else
			{   
				try {
					throw new FileException(" ");
				} catch (FileException e) {
					e.printStackTrace();
				}
				return null;
			}
		} else {
			try {
				throw new FileException(" ");
			} catch (FileException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	
	public File checkisFile(String AbsolutePath) {
		File file = new File(commonPath(AbsolutePath));
		if (file.isAbsolute()) {
			if(file.isFile())
			{
				return file;
			}else
			{   
				try {
					throw new FileException(" ");
				} catch (FileException e) {
					e.printStackTrace();
				}
				return null;
			}
		} else {
			try {
				throw new FileException(" ");
			} catch (FileException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	
	public String suffix(String str) {

		int last = str.lastIndexOf(".");
		String suffixstring = str.substring(last, str.length());
		return suffixstring;
	}
	public String takeOutSuffix(String filename)
	{
		
		String suffix=suffix(filename);
		int length=suffix.length();
		char[] by= filename.toCharArray();
		String str="";
		for(int i=0;i<(by.length-length);i++)
		{
			str+=String.valueOf(by[i]);
		}
		return str.toString().trim();
	}
	public String absolutePathChangeRelative(String  Absolutepath, String realcontextpath) {
		int conLength = realcontextpath.length();
		String str="";
		char[] cr = Absolutepath.toCharArray();
		for (int i = conLength; i < cr.length; i++) {
			if (cr[i] == '\\') {
				cr[i] = '/';
			}
			 str += String.valueOf(cr[i]);
		}
		return str.toString().trim();
	}
 
	
	public static void checkIsADirectory(String path){
		File f = new File(path);
		if(f.exists()){
			if(!f.isDirectory()){
				f.mkdir(); 
			}
		}else{
			f.mkdir(); 
		}
	}
	public String relativePathChangeAbsolutePath(String relativePath,String realcontextpath)
	{   
		String str="";
		char[] cr =relativePath.toCharArray();
		for(int i=0;i<cr.length;i++)
		{
			if(cr[i]=='/')
			{
				cr[i]='\\';
			}
			 str += String.valueOf(cr[i]);
		}
		String AbsolutePath=(realcontextpath+str).toString().trim();
		return AbsolutePath;
	}
	
	
	public String relativePathChangeAbsolutePath(String relativePath,String realcontextpath,String separator)
	{   
		String str="";
		char[] cr =relativePath.toCharArray();
		for(int i=0;i<cr.length;i++)
		{
			if(cr[i]=='/')
			{
				cr[i]='\\';
			}
			 str += String.valueOf(cr[i]);
		}
		String AbsolutePath=(realcontextpath+"\\"+str).toString().trim();
		return AbsolutePath;
	}
	
	
	
	public String urlPathSeparator(HttpServletRequest request)
	{
		StringBuffer sb=new StringBuffer();
		String getRequestURL=request.getRequestURI();
		int number=0;
		if(getRequestURL!=null)
		{
			char[] ch=getRequestURL.toCharArray();
			for(int i=0;i<ch.length;i++)
			{
				if(ch[i]=='/')
				{
					number++;
				}
			}
			
			if((number-2)>=0)
			{
				for(int j=0;j<(number-2);j++)
				{
					sb.append("../");
				}
			}
			
			return sb.toString();
		}else{
			try {
				throw new FileException(" ");
			} catch (FileException e) {
				e.printStackTrace();
			}
			return null;
		}
		
	}
	
	public String getServerAddress(HttpServletRequest request)
	{
		HttpSession hs=request.getSession();
		ServletContext sc=hs.getServletContext();
		String serveraddress=(String)sc.getAttribute("serveraddress");
		
		return serveraddress;
	}
	



	public void cerateDirectory(String path){
		File file = new File(commonPath(path));
		if (file.isAbsolute()) {
			file.mkdir();
		} else {
			try {
				throw new FileException(" ");
			} catch (FileException e) {
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("unused")
	public void deleteFile(String path) throws FileException {
		 
		File file = checkIsAbsolute(path);		

	}
	//删除文件夹
	//param folderPath 文件夹完整绝对路径

	     public static void delFolder(String folderPath) {
	     try {
	        delAllFile(folderPath); //删除完里面所有内容
	        String filePath = folderPath;
	        filePath = filePath.toString();
	        java.io.File myFilePath = new java.io.File(filePath);
	        myFilePath.delete(); //删除空文件夹
	     } catch (Exception e) {
	       e.printStackTrace(); 
	     }
	}

	//删除指定文件夹下所有文件
	//param path 文件夹完整绝对路径
	   public static boolean delAllFile(String path) {
	       boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
	         return flag;
	       }
	       String[] tempList = file.list();
	       File temp = null;
	       for (int i = 0; i < tempList.length; i++) {
	          if (path.endsWith(File.separator)) {
	             temp = new File(path + tempList[i]);
	          } else {
	              temp = new File(path + File.separator + tempList[i]);
	          }
	          if (temp.isFile()) {
	             temp.delete();
	          }
	          if (temp.isDirectory()) {
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;
	          }
	       }
	       return flag;
	     }
	}
 
